package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.apache.dubbo.config.annotation.Service;
import org.aspectj.lang.annotation.AfterReturning;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service(interfaceClass = SetmealService.class)
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> page = findAll(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Setmeal setmeal, Integer[] ids) {
        if(setmealDao.add(setmeal)){
            setSetmealAndCheckgroup(setmeal.getId(),ids);
        }else{
            throw new RuntimeException("添加失败");
        }
    }

    @Override
    public void setSetmealAndCheckgroup(Integer sid, Integer[] ids) {
        if(sid==null||ids==null||ids.length==0||!setmealDao.setSetmealAndCheckgroup(sid,ids)){
            throw new RuntimeException("添加失败");
        }
    }

    @Override
    public Result findSetMealCount(){
        List<Map<String, Object>> setMealCount = setmealDao.findSetMealCount();

        //setmealNames
        //setmealCountm
        List<String> listName = new ArrayList<>();

        for (Map<String, Object> temp : setMealCount) {
            listName.add((String)temp.get("name"));
        }
        Map<String,Object> map = new HashMap<>();
        map.put("setmealNames",listName);
        map.put("setmealCount",setMealCount);

        return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);


//[{months=2019-12, memberCount=1}, {months=2020-11, memberCount=2}]

    }


    @Override
    public void deleteCheckGroupAndCheckItem(Integer gid) {

    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    @Override
    public Page<Setmeal> findAll(String queryString) {
        return setmealDao.findAll(queryString);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {

    }

    @Override
    public void delete(Integer id) {

    }
}
